#include<iostream>
#include<sstream>
#include<string>
#include<memory>
#include<jsoncpp/json/json.h>

void serialize()
{
    const char *name = "Gavin";
    int age = 21;
    float score[] = {150, 149, 148};
    Json::Value val;
    val["Name"] = name;
    val["Age"] = age;
    val["Score"].append(score[0]);
    val["Score"].append(score[1]);
    val["Score"].append(score[2]);

    Json::StreamWriterBuilder swb;

    // 设置 JSON 输出不转义 Unicode 字符
    //swb.settings_["indentation"] = "";  // 去掉缩进
    swb.settings_["ensureAscii"] = false;  // 使中文字符不被转义

    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
    std::stringstream ss;
    int ret = sw->write(val,&ss);
    if(ret != 0)
    {
        std::cout<<"write failed!\n";
        return;
    }
    std::cout << ss.str() <<std::endl;
}

void unserialize(const std::string &str)
{
    Json::Value val;
    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
    std::string err;
    bool ret = cr->parse(str.c_str(),str.c_str()+str.size(),&val,&err);
    if(ret==false)
    {
        std::cout << "parse failed\n";
        return ;
    }
    std::cout << val["Name"] <<std::endl;
    std::cout << val["Age"] <<std::endl;
    int sz = val["Score"].size();
    for(int i=0;i<sz;i++)
        std::cout << val["Score"][i] <<std::endl;
}

int main()
{
    //serialize();
    std::string str = R"({"Name":"Gavin", "Age":"22", "Score":[66, 77, 88]})";
    unserialize(str);
    return 0;
}